package mxmf.ctrl.model;

import java.io.Serializable;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;

import mxmf.data.Permission;

public class QueryParam implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final String FALSE_PARAM = " 1!=1 ";

    public static final Integer DEFAULT_SIZE = 20;
    public static final Integer DEFAULT_PAGE = 0;
    protected Integer page = DEFAULT_PAGE;
    protected Integer size = DEFAULT_SIZE;
    protected String sort;

    protected String[] userCpCodes;
    protected String[] userOpCodes;
    protected String[] userTypeCodes;
    

    protected String keyword;

    protected boolean permission = true;//是否有cp/op/typeCode权限

    public Pageable getPageable() {
        if (StringUtils.isBlank(sort)) {
            return PageRequest.of(page, size);
        }
        String[] sortStr = sort.split(",");
        if (sortStr.length < 2) {
            return PageRequest.of(page, size);
        }
        return PageRequest.of(page, size, Direction.fromString(sortStr[1]), sortStr[0]);
    }

    public Integer getOffset() {
        return page * size;
    }

    /**
     * @return the page
     */
    public Integer getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(Integer page) {
        this.page = page;
    }

    /**
     * @return the size
     */
    public Integer getSize() {
        return size;
    }

    /**
     * @param size the size to set
     */
    public void setSize(Integer size) {
        this.size = size;
    }

    /**
     * @return the sort
     */
    public String getSort() {
        return sort;
    }

    /**
     * @param sort the sort to set
     */
    public void setSort(String sort) {
        this.sort = sort;
    }

    /**
     * @return the keyword
     */
    public String getKeyword() {
        return keyword;
    }

    /**
     * @param keyword the keyword to set
     */
    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    /**
     * @return the cpCodes
     */
    public String[] getUserCpCodes() {
        if (ArrayUtils.isEmpty(userCpCodes)) {
            return null;
        }
        for (String cpCode : userCpCodes) {
            if (cpCode.equals(Permission.STAR)) {
                return null;
            }
        }
        return userCpCodes;
    }

    public String[] getUserOpCodes() {
        if (ArrayUtils.isEmpty(userOpCodes)) {
            return null;
        }
        for (String opCode : userOpCodes) {
            if (opCode.equals(Permission.STAR)) {
                return null;
            }
        }
        return userOpCodes;
    }
    public String[] getUserTypeCodes() {
        if (ArrayUtils.isEmpty(userTypeCodes)) {
            return null;
        }
        for (String typeCode : userTypeCodes) {
            if (typeCode.equals(Permission.STAR)) {
                return null;
            }
        }
        return userTypeCodes;
    }

    /**
     * @param cpCodes the cpCodes to set
     */
    public void setUserCpCodes(String[] cpCodes) {
        if (ArrayUtils.isEmpty(cpCodes)) {
            this.permission = false;
        }
        this.userCpCodes = cpCodes;
    }

    public void setUserOpCodes(String[] opCodes) {
        if (ArrayUtils.isEmpty(opCodes)) {
            this.permission = false;
        }
        this.userOpCodes = opCodes;
    }
    public void setUserTypeCodes(String[] typeCodes) {
        if (ArrayUtils.isEmpty(typeCodes)) {
            this.permission = false;
        }
        this.userTypeCodes = typeCodes;
    }

    public boolean isPermission() {
        return permission;
    }

    public void setPermission(boolean permission) {
        this.permission = permission;
    }

}
